Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
@thi.ng/random
Advanced tools
Pseudo-random number generators w/ unified API, distributions, weighted choices, ID generation
@thi.ng/random is a versatile library for generating random numbers and sequences. It provides various random number generators (RNGs) and utilities for creating random values, sequences, and distributions.
Basic Random Number Generation
This feature allows you to generate basic random numbers, both floating-point and integer values, using the default system RNG.
const { SYSTEM } = require('@thi.ng/random');
const rng = SYSTEM;
console.log(rng.float()); // Generates a random float between 0 and 1
console.log(rng.int()); // Generates a random integer
Seeded Random Number Generation
This feature allows you to create a seeded random number generator, which produces reproducible sequences of random numbers.
const { XsAdd } = require('@thi.ng/random');
const rng = new XsAdd(12345); // Seeded RNG
console.log(rng.float()); // Generates a reproducible random float
console.log(rng.int()); // Generates a reproducible random integer
Random Value Generation from Arrays
This feature allows you to randomly select values from an array, which is useful for shuffling or sampling data.
const { SYSTEM } = require('@thi.ng/random');
const rng = SYSTEM;
const arr = [1, 2, 3, 4, 5];
console.log(rng.choice(arr)); // Randomly selects an element from the array
Random Distributions
This feature allows you to generate random numbers from various statistical distributions, such as normal (Gaussian) distributions.
const { SYSTEM } = require('@thi.ng/random');
const rng = SYSTEM;
console.log(rng.norm(0, 1)); // Generates a random number from a normal distribution with mean 0 and standard deviation 1
random-js is a library for generating random numbers and values. It provides a variety of RNGs and utilities for creating random values, similar to @thi.ng/random. However, random-js focuses more on providing a simple API for common randomization tasks.
seedrandom is a library for seeded random number generation. It allows you to create reproducible random sequences by providing a seed. While it offers fewer utilities compared to @thi.ng/random, it is highly focused on the quality and reproducibility of random numbers.
chance is a library for generating random data for various purposes, including numbers, strings, and even entire objects. It offers a broader range of random data generation compared to @thi.ng/random, which is more focused on numerical randomization.
This project is part of the @thi.ng/umbrella monorepo.
Pseudo-random number generators w/ unified API, distributions, weighted choices, ID generation.
This package provides the IRandom
interface and various (mostly seedable)
pseudo-random number generator implementations, incl. IRandom
wrappers for
Math.random()
and window.crypto
:
Partially ported from C implementations taken from c.thi.ng.
coin()
/ fairCoin()
randomBytes()
/ randomBytesFrom()
randomID()
weightedRandom()
uniqueIndices()
/ uniqueValuesFrom()
uuidv4Bytes()
/ uuid()
STABLE - used in production
Search or submit any issues for this package
yarn add @thi.ng/random
// ES module
<script type="module" src="https://unpkg.com/@thi.ng/random?module" crossorigin></script>
// UMD
<script src="https://unpkg.com/@thi.ng/random/lib/index.umd.js" crossorigin></script>
Package sizes (gzipped, pre-treeshake): ESM: 1.81 KB / CJS: 1.95 KB / UMD: 1.91 KB
Several demos in this repo's /examples directory are using this package.
A selection:
Screenshot | Description | Live demo | Source |
---|---|---|---|
Probabilistic color theme generator | Demo | Source | |
Interactive inverse FFT toy synth | Demo | Source | |
Interactive pixel sorting tool using thi.ng/color & thi.ng/pixel | Demo | Source | |
Evolutionary shader generation using genetic programming | Demo | Source |
import { Smush32 } from "@thi.ng/random";
const rnd = new Smush32(0xdecafbad);
// the following methods are available for all generators
// next uint (0 .. 2^32-1)
rnd.int()
// 4022849029
// next float [0.0 .. 1.0)
rnd.float()
// 0.2698542904737066
// next normalized float (w/ opt scale)
// [-scale .. +scale)
rnd.norm(100)
// 57.70723665079737
// next float in given interval [min .. max)
rnd.minmax(10, 20)
// 15.295951807707537
Karsten Schmidt
If this project contributes to an academic publication, please cite it as:
@misc{thing-random,
title = "@thi.ng/random",
author = "Karsten Schmidt",
note = "https://thi.ng/random",
year = 2015
}
© 2015 - 2021 Karsten Schmidt // Apache Software License 2.0
FAQs
Pseudo-random number generators w/ unified API, distributions, weighted choices, ID generation
The npm package @thi.ng/random receives a total of 47,701 weekly downloads. As such, @thi.ng/random popularity was classified as popular.
We found that @thi.ng/random demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.